package org.hackystat.projectbrowser.page.chooseTAP;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.hackystat.projectbrowser.ProjectBrowserSession;
import org.hackystat.projectbrowser.page.settrigger.ChooseTrigger;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.TriggerActionPair;

/**
 * Creates the table for choosing the trigger action pairs.
 * 
 * @author Edward Kim
 * @author Sonwright M. Gomez
 * 
 */
public class ChooseTAPTable extends ListView {

  /** Supports serialization. */
  private static final long serialVersionUID = 1L;

  private List<TriggerActionPair> pairs = new ArrayList<TriggerActionPair>();
  /** Displays text at the bottom of the screen for user information. */
  private Project project;

  /**
   * Constructor
   * 
   * @param id the Id.
   * @param projectName the project Name.
   */
  public ChooseTAPTable(String id, String projectName) {
    super(id);

    project = ProjectBrowserSession.get().getProjectTypeXMLByNameId(projectName);

    if (project.getTriggerActionPairs() == null) {
      System.out.println("No Trigger Action pairs available.");
    }

    else {
      pairs = project.getTriggerActionPairs().getTriggerActionPair();
      setList(pairs);
    }

  }

  /**
   * Populates the list of TAP with their required links.
   * 
   * @param item the item to view
   */
  @Override
  protected void populateItem(ListItem item) {
    TriggerActionPair pair = (TriggerActionPair) item.getModelObject();
    item.add(new Label("TAPName", pair.getId()));
    // FOR NOW POINT TO THE SET TRIGGER PAGE BECAUSE WE HAVE NO PAGE TO POINT TO

    PageParameters param2 = new PageParameters();
    param2.put("projectName", project.getProjectName());
    // item.add(new BookmarkablePageLink("NewTAP", NewTAP.class, param2));
    param2.put("tapName", pair.getId());
    item.add(new BookmarkablePageLink("EditId", EditTAP.class, param2));
    item.add(new BookmarkablePageLink("projectLink", ChooseTrigger.class, param2));
    item.add(new DeleteTriggerActionPairLink("deleteTriggerActionPair", pair.getId(), project
        .getProjectName()));
  }

}
